import os

from bean.Constants import Constants
from bean.TableInfo import TableInfo

from builder.CommentBuilder import CommentBuilder
from utils.StringUtils import uppercase_first_char


class PoBuilder:

    @staticmethod
    def execute_build(table_info: TableInfo) -> None:
        if not os.path.exists(Constants.get_instance().PO_PATH):
            os.makedirs(Constants.get_instance().PO_PATH)

        with open(Constants.get_instance().PO_PATH + f"{table_info.bean_name}.java", "w", encoding="utf-8") as f:
            """
            导包和定义包名
            """
            f.write(f"package {Constants.get_instance().PACKAGE_PO};\n\n")
            if table_info.have_date or table_info.have_date_time:

                f.write("import com.fasterxml.jackson.annotation.JsonFormat;\n")
                f.write("import org.springframework.format.annotation.DateTimeFormat;\n")
                f.write(f"\nimport {Constants.get_instance().PACKAGE_UTILS}.DateUtils;\n")
                f.write(f"import {Constants.get_instance().PACKAGE_ENUM}.DateTimePatternEnum;\n")
                f.write("\nimport java.util.Date;\n")
            if table_info.have_big_decimal:
                f.write("import java.math.BigDecimal;\n")
            f.write("import java.io.Serializable;\n")
            if set(Constants.get_instance().IGNORE_BEAN_TOJSON_FILED) & set(list(table_info.field_map.keys())) != set():
                f.write(f"{Constants.get_instance().IGNORE_BEAN_TOJSON_CLASS}\n")
            CommentBuilder.build_class_comment(f, table_info.comment)
            f.write("\n")
            """
            开始定义类
            """
            f.write(f"public class {table_info.bean_name} implements Serializable{{\n")

            """
            遍历table_info的字段写入table_info的类中
            """
            for j in table_info.field_map.values():
                f.write("\n")
                CommentBuilder.build_comment_and_format(f, j)
                f.write(f"\tprivate {j.java_type} {j.property_name};")
                f.write("\n")
            for j in table_info.field_map.values():
                f.write("\n")
                f.write(f"\tpublic {j.java_type} get{uppercase_first_char(j.property_name)}(){{\n")
                f.write(f"\t\treturn this.{j.property_name};")
                f.write("\n\t}\n")
                f.write("\n")
                f.write(
                    f"\tpublic void set{uppercase_first_char(j.property_name)}({j.java_type} {j.property_name}){{\n")
                f.write(f"\t\tthis.{j.property_name} = {j.property_name};")
                f.write("\n\t}")
                f.write("\n")

            """
            生成toString方法
            """

            to_string = ""
            for j in table_info.field_map.values():
                if j.java_type != "String":
                    if j.sql_type == "datetime":
                        to_string += f"\"{j.property_name}:\\t\"+DateUtils.format({j.property_name},DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern())+\"\\n\"+"
                    elif j.sql_type == "date":
                        to_string += f"\"{j.property_name}:\\t\"+DateUtils.format({j.property_name},DateTimePatternEnum.YYYY_MM_DD.getPattern())+\"\\n\"+"
                    else:
                        to_string += f"\"{j.property_name}:\\t\"+({j.property_name}==null?\"空\":{j.property_name}.toString())+\"\\n\"+"
                    continue
                to_string += f"\"{j.property_name}:\\t\"+({j.property_name}==null?\"空\":{j.property_name})+\"\\n\"+"
            to_string = to_string[0:-1]
            f.write("\n\t@Override\n")
            f.write("\tpublic String toString(){\n")
            f.write(f"\t\treturn {to_string};")
            f.write("\n")
            f.write("\t}")

            f.write("\n}")


if __name__ == "__main__":
    pass